<?php

	// register widget
	add_action('widgets_init', create_function('', 'return register_widget("VnNews_PopularNews");'));

	// Creating the widget
	class VnNews_PopularNews extends WP_Widget {

		function __construct() {
			parent::__construct(
				'VnNews_PopularNews',
				__('VnNews Popular News', 'vnnews_widget_popularnews'),
				array( 'description' => __( 'Show popular news and lastest news', 'vnnews_widget_popularnews' ), )
			);
		}

		// Creating widget front-end
		// This is where the action happens
		public function widget( $args, $instance ) {
			global $wpdb;

			$lastest_title = apply_filters( 'widget_lastest_title', $instance['lastest_title'] );
			$popular_title = apply_filters( 'widget_popular_title', $instance['popular_title'] );
			$number_of_news = apply_filters( 'widget_number_of_news', $instance['number_of_news'] );

			$str .= '<div class="vnnews-tabs">';
			$str .= '<ul><li><a href="#tabs-1">'. $popular_title .'</a></li>';
			$str .= '<li><a href="#tabs-2">'. $lastest_title .'</a></li></ul>';


			//Display popular news
			$arg = array('posts_per_page'=>$number_of_news,"post_type"=>"post",
							"post_status"=>"publish",
							"ignore_sticky_posts" => 1,
							"order" => 'DESC',
							"meta_key" => "post_views_count",
							"orderby" => "meta_value");

            WP_Reset_Query();
            query_posts($arg);

            if(have_posts()):

				$str .= "<div id='tabs-1'>";

				while(have_posts()): the_post();
                    $post_id = get_the_ID();
					$link = get_permalink();
					$title = the_title('','',FALSE);
					$short_title = mb_substr($title, 0, 60);
					if (strlen($title) > 60){
						$short_title .= ' ...';
					}
					$num_comments = get_comments_number($post_id);
					$post_view = getPostViews($post_id);

					$str .= '<div class="post-item">';
					if ( has_post_thumbnail() ) {
						$str .= '<div class="thumb">';
						$str .= '<a href="'. $link .'">'. get_the_post_thumbnail($post_id, array(75,75) ) . '</a>';
						$str .= '</div>';
					}

					$str .= '<div class="post-right">';
					$str .= '<a href="'. $link .'" rel="bookmark">'. $short_title .'</a>';
					$str .= '</div>';

					$str .= '</div>';

                endwhile;

                $str .= "</div>";

        	endif;

        	WP_Reset_Query();

        	//Display lastest news
        	$arg = array('posts_per_page'=>$number_of_news,"post_type"=>"post",
							"post_status"=>"publish",
							"ignore_sticky_posts" => 1);

        	query_posts($arg);

            if(have_posts()):

				$str .= "<div id='tabs-2'>";

				while(have_posts()): the_post();
                    $post_id = get_the_ID();
					$link = get_permalink();
					$title = the_title('','',FALSE);
					$short_title = mb_substr($title, 0, 60);
					if (strlen($title) > 60){
						$short_title .= ' ...';
					}
					$num_comments = get_comments_number($post_id);
					$post_view = getPostViews($post_id);

					$str .= '<div class="post-item">';
					if ( has_post_thumbnail() ) {
						$str .= '<div class="thumb">';
						$str .= '<a href="'. $link .'">'. get_the_post_thumbnail($post_id, array(75,75) ) . '</a>';
						$str .= '</div>';
					}

					$str .= '<div class="post-right">';
					$str .= '<a href="'. $link .'" rel="bookmark">'. $short_title .'</a>';
					$str .= '</div>';

					$str .= '</div>';

                endwhile;

                $str .= "</div>";

        	endif;

        	$str .= "</div>";

        	echo $args['before_widget'];

        	echo $str;

        	echo $args['after_widget'];

		}

		// Widget Backend
		public function form( $options ) {

			global $wpdb;
            // options exist? if not set defaults
            if ( !is_array($options))
            {
                $options = array('lastest_title'=> __(''), 'popular_title'=>'', 'number_of_news'=>'');
            }

            echo '<p>
				<label for="'. $this->get_field_id('lastest_title') .'">'. __('Lastest news title') .'
				<input id="'. $this->get_field_id('lastest_title') .'" name="'. $this->get_field_name('lastest_title') .'" type="text" class="widefat" value="'.strip_tags($options['lastest_title']).'" /></label></p>';

        	echo '<p>
				<label for="'. $this->get_field_id('popular_title') .'">'. __('Popular news title') .'
				<input id="'. $this->get_field_id('popular_title') .'" name="'. $this->get_field_name('popular_title') .'" type="text" class="widefat" value="'.strip_tags($options['popular_title']).'" /></label></p>';

			echo '<p>
				<label for="'. $this->get_field_id('number_of_news') .'">'. __('Number of news') .'
				<input id="'. $this->get_field_id('number_of_news') .'" name="'. $this->get_field_name('number_of_news') .'" type="text" class="widefat" value="'.strip_tags($options['number_of_news']).'" /></label></p>';
		}

		// Updating widget replacing old instances with new
		public function update( $new_instance, $old_instance ) {
			$options = $old_instance;
            $options['lastest_title'] = strip_tags($new_instance['lastest_title']);
            $options['popular_title'] = strip_tags($new_instance['popular_title']);
            $options['number_of_news'] = intval($new_instance['number_of_news']);
            return $options;
		}
	}
	// Class VnNews_PopularNews ends here
?>